package com.ctvit.cch.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.cch.core.mapper.HotCategoryFavoriteMapper;
import com.ctvit.cch.core.mapper.HotCategoryMapper;
import com.ctvit.cch.core.pojo.domain.HotCategory;
import com.ctvit.cch.core.pojo.domain.HotCategoryFavorite;
import com.ctvit.cch.core.pojo.dto.hot.HotCategoryAddDTO;
import com.ctvit.cch.core.pojo.dto.hot.HotCategoryEditDTO;
import com.ctvit.cch.core.pojo.dto.hot.HotCategoryListDTO;
import com.ctvit.cch.core.pojo.vo.hot.HotCategoryGetVO;
import com.ctvit.cch.core.pojo.vo.hot.HotCategoryListVO;
import com.ctvit.cch.core.pojo.vo.hot.HotCategoryRecordVO;
import com.ctvit.cch.core.service.IHotCategoryService;
import com.ruoyi.common.utils.SecurityUtils;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 热点分类Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/7/19
 */
@Service
public class HotCategoryServiceImpl extends ServiceImpl<HotCategoryMapper, HotCategory> implements IHotCategoryService {

    private final HotCategoryFavoriteMapper hotCategoryFavoriteMapper;

    public HotCategoryServiceImpl(HotCategoryFavoriteMapper hotCategoryFavoriteMapper) {
        this.hotCategoryFavoriteMapper = hotCategoryFavoriteMapper;
    }

    @Override
    public List<HotCategoryListVO> list(HotCategoryListDTO dto) {
        LambdaQueryWrapper<HotCategory> qWrapper = new LambdaQueryWrapper<>();
        qWrapper.like(StringUtils.isNotBlank(dto.getCategoryName()), HotCategory::getCategoryName,
                dto.getCategoryName());
        qWrapper.eq(StringUtils.isNotBlank(dto.getStatus()), HotCategory::getStatus, dto.getStatus());
        qWrapper.eq(StringUtils.isNotBlank(dto.getShowFlag()), HotCategory::getShowFlag, dto.getShowFlag());

        List<HotCategory> results = list(qWrapper);

        Long excludeId = dto.getExcludeId();
        if (Objects.nonNull(excludeId)) {
            results.removeIf(d -> excludeId.equals(d.getCategoryId())
                    || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), String.valueOf(excludeId)));
        }

        return results.stream().map(t -> {
            HotCategoryListVO v = new HotCategoryListVO();
            BeanUtils.copyProperties(Objects.requireNonNull(t), v);
            return v;
        }).collect(Collectors.toList());
    }

    @Override
    public HotCategoryGetVO get(String id) {
        HotCategoryGetVO target = new HotCategoryGetVO();
        BeanUtils.copyProperties(Objects.requireNonNull(getById(id)), target);
        return target;
    }

    @Override
    public boolean add(HotCategoryAddDTO dto) {
        HotCategory entity = new HotCategory();
        BeanUtils.copyProperties(Objects.requireNonNull(dto), entity);
        return save(entity);
    }

    @Override
    public boolean edit(HotCategoryEditDTO dto) {
        HotCategory entity = new HotCategory();
        BeanUtils.copyProperties(Objects.requireNonNull(dto), entity);
        return updateById(entity);
    }

    @Override
    public List<HotCategoryRecordVO> record() {
        List<Long> favorites = hotCategoryFavoriteMapper.selectList(new LambdaQueryWrapper<HotCategoryFavorite>()
                .select(HotCategoryFavorite::getCategoryId)
                .eq(HotCategoryFavorite::getUserId, SecurityUtils.getUserId())).stream()
                .map(HotCategoryFavorite::getCategoryId).collect(Collectors.toList());

        LambdaQueryWrapper<HotCategory> qWrapper = new LambdaQueryWrapper<>();
        qWrapper.select(HotCategory::getCategoryId, HotCategory::getCategoryName);
        qWrapper.eq(HotCategory::getStatus, NumberUtils.INTEGER_ZERO.toString());
        qWrapper.ne(HotCategory::getParentId, NumberUtils.INTEGER_ZERO);
        qWrapper.in(CollectionUtils.isNotEmpty(favorites), HotCategory::getCategoryId, favorites);
        qWrapper.orderByAsc(HotCategory::getOrderNum);

        return list(qWrapper).stream().map(t -> {
            HotCategoryRecordVO v = new HotCategoryRecordVO();
            BeanUtils.copyProperties(Objects.requireNonNull(t), v);
            return v;
        }).collect(Collectors.toList());
    }

    @Override
    public List<HotCategoryRecordVO> all() {
        LambdaQueryWrapper<HotCategory> qWrapper = new LambdaQueryWrapper<>();
        qWrapper.select(HotCategory::getCategoryId, HotCategory::getCategoryName);
        qWrapper.eq(HotCategory::getStatus, NumberUtils.INTEGER_ZERO.toString());
        qWrapper.eq(HotCategory::getShowFlag, NumberUtils.INTEGER_ONE.toString());
        qWrapper.orderByAsc(HotCategory::getOrderNum);

        return list(qWrapper).stream().map(t -> {
            HotCategoryRecordVO v = new HotCategoryRecordVO();
            BeanUtils.copyProperties(Objects.requireNonNull(t), v);
            return v;
        }).collect(Collectors.toList());
    }

    @Override
    public List<HotCategoryRecordVO> my() {
        List<Long> favorites = hotCategoryFavoriteMapper.selectList(new LambdaQueryWrapper<HotCategoryFavorite>()
                .select(HotCategoryFavorite::getCategoryId)
                .eq(HotCategoryFavorite::getUserId, SecurityUtils.getUserId())).stream()
                .map(HotCategoryFavorite::getCategoryId).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(favorites)) {
            return Collections.emptyList();
        }

        LambdaQueryWrapper<HotCategory> qWrapper = new LambdaQueryWrapper<>();
        qWrapper.select(HotCategory::getCategoryId, HotCategory::getCategoryName);
        qWrapper.eq(HotCategory::getStatus, NumberUtils.INTEGER_ZERO.toString());
        qWrapper.eq(HotCategory::getShowFlag, NumberUtils.INTEGER_ONE.toString());
        qWrapper.in(HotCategory::getCategoryId, favorites);
        qWrapper.orderByAsc(HotCategory::getOrderNum);

        return list(qWrapper).stream().map(t -> {
            HotCategoryRecordVO v = new HotCategoryRecordVO();
            BeanUtils.copyProperties(Objects.requireNonNull(t), v);
            return v;
        }).collect(Collectors.toList());
    }
}
